package com.bosch.in.rtp1.ept.manager.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.bosch.in.rtp1.ept.action.form.FrozenColumnForm;
import com.bosch.in.rtp1.ept.constant.TableType;
import com.bosch.in.rtp1.ept.dao.FrozenColumnDao;
import com.bosch.in.rtp1.ept.manager.FrozenColumnManager;
import com.bosch.in.rtp1.ept.util.DaoFactory;
import com.bosch.in.rtp1.ept.util.ehcache.EhcacheFactory;

/**
 * Concrete implementation of FrozenColumnManager which provides service to manipulate 
 * Frozen Column Configuration data, delegate data source retrieval and transaction management to 
 * DBConnectionManager attribute.  
 * @author TPR1HC
 *
 */

public class FrozenColumnManagerImpl implements FrozenColumnManager{
	
	private static final Logger LOGGER = Logger.getLogger(FrozenColumnManagerImpl.class);
	
	private static final String CACHE_KEY = "FrozenColumnManagerImpl";
	
	/**
	 * used to manage connection
	 */
	private DBConnectionManager dbManager;
	
	/**
	 * dao object
	 */
	private FrozenColumnDao frozenColumnDao;
	
	public FrozenColumnManagerImpl(){
		
	}

	/**
	 * lazy initialize connection when we really need to interact with database and in case 
	 * data can't be found in caching system.
	 * @throws SQLException
	 */
	private void init() throws SQLException {
		if (dbManager == null) {
			dbManager = new DBConnectionManager();
			frozenColumnDao = DaoFactory.getInstance().getFrozenColumnDao(dbManager.getConnection());
		}
	}


	@Override
	public boolean updateFrozenColumn(FrozenColumnForm frozenColumnForm) throws SQLException {
		LOGGER.debug("Begin updateFrozenColumn");
		try
		{
			// lazy init connection
			init();
			// clear cache
			EhcacheFactory.getFrzColcacheWrapper().invalidate(CACHE_KEY);
			
			frozenColumnDao.clearStartScrollingColumn();
			
			return frozenColumnDao.setStartScrollingColumn(frozenColumnForm.getScrollingColumnList());
		}
		finally
		{
			dbManager.commit();
			LOGGER.debug("Commited updateFrozenColumn");
			dbManager.close();
			LOGGER.debug("Closed connection");
			LOGGER.debug("End updateFrozenColumn");
		}
	}


	@Override
	public FrozenColumnForm getFrozenColumnForm() throws SQLException {
		LOGGER.debug("Begin getFrozenColumnForm");
		try
		{
			FrozenColumnForm frzColForm = EhcacheFactory.getFrzColcacheWrapper().get(CACHE_KEY);
			if (frzColForm == null) {
				// lazy init connection
				init();
				frzColForm = frozenColumnDao.getFrozenColumnForm();
				EhcacheFactory.getFrzColcacheWrapper().put(CACHE_KEY, frzColForm);
			}
			return frzColForm;
		}
		finally
		{
			if (dbManager != null) {
				dbManager.close();
				LOGGER.debug("Closed connection");
			}
			LOGGER.debug("End getFrozenColumnForm");
		}
	}


	@Override
	public List<String> getFrozenColumns(TableType tableType)
			throws SQLException {
		List<String> frzCols;
		
		FrozenColumnForm frzColForm = getFrozenColumnForm();
		if (tableType == TableType.LOT_STATUS) {
			List<String> cols = frzColForm.getLotStatusCols();
			
			frzCols = getLeftCols(frzColForm.getLotStatusStartScrollingCol(), cols);
		} else {
			List<String> cols = frzColForm.getReleasedHoldCols();
			
			frzCols = getLeftCols(frzColForm.getReleasedHoldStartScrollingCol(), cols);
		}
		
		return frzCols;
	}


	/**
	 * 
	 * @param scrollCol
	 * @param cols
	 * @return
	 */
	private List<String> getLeftCols(String scrollCol,
			List<String> cols) {
		List<String> frzCols = new ArrayList<String>();
		
		// If scroll is empty, there is no frozen column
		if (scrollCol != null && !scrollCol.isEmpty()) {
			for(int i = 1; i < cols.size(); i++) {
				if (!cols.get(i).equals(scrollCol)) {
					frzCols.add(cols.get(i));
				} else {
					break;
				}
			}
		}
		
		return frzCols;
	}

}
